<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Lab #1: Straight-line Program Interpreter</title>
<link rel="stylesheet" type="text/css">
</head>

<body>
<font face="Trebuchet MS">
<table width="100%">
<tbody>
  <tr>
<td>
<h2>Lab #1: Straight-line Program Interpreter</h2>
<hr>


<h3>Software Setup </h3>
<p>
In this lab, we need to use a ML compiler, such as SML/NJ, and a
text editor, such as Emacs.
You  can down load some of them 
<a href="http://staff.ustc.edu.cn/~bjhua/courses/compiler/2011/tools.html">here</a>.
</p>

<p>
After you download and install these tools, you can start 
to program with ML. Take SML/NJ for example, if you use command
mode (both in Linux and Windows), you can start sml by typing 
<code>sml</code> at a prompt. If you use Emacs, you can press 
<code>Alt-X</code>, type <code>run-sml</code>, and then press 
<code>Enter</code>, after these three steps, the sml will start. 
When sml starts, it will output the following characters
</p>
  <pre>     %sml
     Standard  ML of New Jersey v110.72 [&hellip;]
     -   </pre>    
     
     
<h3>Introduction</h3>
<p>
This lab requires you to write a simple program analyzer and 
interpreter for a straight-line programming language. This 
exercise serves as an introduction to environments (symbol 
tables mapping variable-names to information about the variables);
to abstract syntax (data structures representing the phrase 
structure of programs); to recursion over tree data structures,
useful in many parts of a compiler; and a functional style of 
programming without assignment statements. The program used in
this lab is followed. 
</p>
<pre>     
     a = 5+3; b = ( print(a,a-1), 10*a ); print(b)
</pre>
<p>In this lab, you will write a simple program analyzer and 
interpreter for this simple program.</p>
<p>You should&nbsp;download&nbsp;this 
<a href="http://staff.ustc.edu.cn/~bjhua/courses/compiler/2011/labs/lab1/lab1.rar">package</a>,  
in which we have offered you a code skeleton. Here is a brief 
discription of these files:
</p>
<table valign="top" border="1" >
  <tr>
    <td >sources.cm:</td>
    <td >makefile</td>
  </tr>
  <tr>
    <td>test.sml:</td>
    <td>the entry point of  whole program</td>
  </tr>
  <tr>
    <td>testprog.sml:</td>
    <td>construct AST  for the program given above </td>
  </tr>
  <tr>
    <td>maxarg.sml:</td>
    <td>read AST of a  program, and return the 
maximum number of arguments of any print statement  within 
any subexpression of the program</td>
  </tr>
  <tr>
    <td>slp.sml:</td>
    <td>definition of  structure SLP, constructors 
of which represent nodes in AST</td>
  </tr>
  <tr>
    <td>interp.sml:</td>
    <td>read AST of a program, and interpret the program </td>
  </tr>
</table>
<h3>Your Job</h3>
<p>
Your job is to finish two files <code>maxarg.sml</code>
and <code>interp.sml</code>. The code you should modify looks like:
</p>
  <pre>     raise Unimplement</pre>
So you should find all place marked as this and replace them with your code.


<h3>Steps</h3>
<ol>
  <li>
  Read Chapter 1 of the textbook, 
  <a href="http://staff.ustc.edu.cn/~bjhua/courses/compiler/2011/tiger.chm">
  <i>Modern Compiler Implementation in Java</i></a>, particularly pages 6-14.
  </li>
  <li>Download the lab file and read  them. Pay special attention 
  to how it is divided into separate structures.</li>
  <li>Install SML/NJ on your own  machine.</li>
  <li>Run sml in the same directory  as your lab1 files.</li>
  <li>At the sml prompt, type <code>CM.make "sources.cm";</code> 
  (semicolon and all). This will compile and load all the source 
  files. Still in SML/NJ, type <code>Test.maxarg();</code> (semicolon and all). 
  This will run the maxarg function on the test program; since 
  you haven't yet modified the  program I provided you, it will 
  raise an exception Unimplemented.</li>
  <li>Edit maxarg.sml until it's the  right program. Still in 
  the same session of sml, type <code>CM.make "sources.cm";</code>  
  Observe the compiler's messages and fix the problems that are 
  reported. You can  keep a running ML session while you are 
  debugging. Simply type <code>CM.make "sources.cm";</code> again 
  when you think you 
  have fixed the problem. This procedure saves a lot of time,  
  because you avoid starting ML over and over again.</li>
  <li>Test your code by invoking  <code>Test.maxarg();</code> If your code 
  still does not work as intended, then debug,  recompile, 
  rerun. Repeat as necessary.</li>
  <li>Do the same for interp.sml,  using <code>CM.make "sources.cm";</code> 
  (semicolon and all)
  for compiling, and <code>Test.interp();</code>(semicolon and all) for running.</li>
  <li>To quit the top-level environment, 
  type <code>ctrl-D</code> (<code>ctrl-Z</code> if using SML under Windows).
  </li>
</ol>
<h3>In-Deep</h3>
<p>This part will present more details on compiling and running the project:
<ul>
  <li>You may treat that <code>CM.make</code> command 
  functions as <code>make</code> in Linux,  and <code>.cm</code>
  file as makefile in  Linux, though there is a little
  difference between them. If you are not  farmiliar with make
  and makefile, don't worry, it doesn't matter, you can skip it. 
  You can just type command
    <pre>CM.make "sources.cm";</pre>
    to compile files listed in <code>sources.cm</code>.<br>
  <li>File <code>test.sml</code> contains the entry of this project, and
  it contains a structure called <code>Test</code> and 
  two functions. You may type command
  <pre>Test.interp();</pre>
  in SML/NJ prompt to run this function, it will call  
	<code>interp()</code> function defined in the <code>interp.sml</code>
 to interpret the AST, which is in turn built by <code>TestProg.prog</code> defined 
	in <code>testprog.sml</code>. And you can use command
    <pre>Test.maxarg();</pre>
    to run this function, it  will call <code>maxarg()</code> defined in the 
	<code>maxarg.sml</code> to calculate the maximum argument numbers of
	an AST, which is also built by 
	<code>TestProg.prog</code> defined in <code>testprog.sml</code>.
</ul>

<h3>Handin</h3>
This completes the lab. Hand in your solution to information system.

</td>
</tr>
  
</tbody>
</table>
</font>


</body>
</html>
